package com.shop.front.dao.store;

import com.shop.jpa.BaseDao;
import com.shop.domain.store.StoreOrder;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public interface StoreOrderDao extends BaseDao<StoreOrder, Long> {

    StoreOrder findByOrderNo(String orderNo);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set payStatus = 1, status = 10, payPrice = ?3, payTime = now() where payStatus = 0 and id = ?1 and orderNo = ?2 ")
    int updateByPaySuccess(Long id, String orderNo, BigDecimal payPrice);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set payStatus = 1, status = 40, payPrice = ?3, payTime = now() where payStatus = 0 and id = ?1 and orderNo = ?2 ")
    int updateCompleteByPaySuccess(Long id, String orderNo, BigDecimal payPrice);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set payStatus = -2, status = -50 where payStatus = 0 and id = ?1 and orderNo = ?2 ")
    int updateStatusByPayTimeout(Long id, String orderNo);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set status = -50 where id = ?1 and status = 0 ")
    int updateStatusByCancel(Long id);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set isUserDel = 1 where id = ?1 ")
    int updateStatusByUserDel(Long id);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set status = 30 where id = ?1 and status = 20 ")
    int updateConfirmReceipt(Long id);

    @Modifying(clearAutomatically = true)
    @Query("update StoreOrder set status = 40 where id = ?1 and status = 30 ")
    int updateStatusByDiscussFinish(Long id);


    @Query(nativeQuery = true, value = "select count(*) from t_store_order where uid =?1  and is_deleted = 0")
    Integer myOrderCount(Long userId);

    @Query(nativeQuery = true, value = "select count(*) from t_store_order where uid =?1 and status= 0  and is_deleted = 0")
    Integer myPayOrderCount(Long userId);
}
